<?php
    /**
    * This is the model class for table "customer_create_by_phone".
    *
    * The followings are the available columns in table 'user_create_by_phone':
    * @property string $id
    * @property string $code
    * @property string $phone
    * @property string $name
    * @property string $password
    * @property string $created
    */
    class CustomerPhoneCode extends Model
    {
        /**
        * Returns the static model of the specified AR class.
        * @param string $className active record class name.
        * @return UserCreateByPhone the static model class
        */
        public static function model($className=__CLASS__)
        {
            return parent::model($className);
        }

        /**
        * @return string the associated database table name
        */
        public function tableName()
        {
            return 'customer_phone_code';
        }

        /**
        * @return array validation rules for model attributes.
        */
        public function rules()
        {
            // NOTE: you should only define rules for those attributes that
            // will receive user inputs.
            return array(
                array('code, phone', 'required'),
                array('code', 'length', 'max'=>4),
                array('phone', 'length', 'max'=>10),
                array('name, password', 'length', 'max'=>255),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id, code, phone, name, password, created', 'safe', 'on'=>'search'),
            );
        }

        /**
        * @return array relational rules.
        */
        public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
            );
        }

        /**
        * @return array customized attribute labels (name=>label)
        */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'code' => 'Code',
                'phone' => 'Phone',
                'name' => 'Name',
                'password' => 'Password',
                'created' => 'Created',
            );
        }

        public function beforeSave(){

            if($this->isNewRecord){
                $this->created = MyDateTime::getCurrentTime();
            }

            return parent::beforeSave();
        }

        /**
        * Retrieves a list of models based on the current search/filter conditions.
        * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
        */
        public function search()
        {
            // Warning: Please modify the following code to remove attributes that
            // should not be searched.

            $criteria=new CDbCriteria;

            $criteria->compare('id',$this->id,true);
            $criteria->compare('code',$this->code,true);
            $criteria->compare('phone',$this->phone,true);
            $criteria->compare('name',$this->name,true);
            $criteria->compare('password',$this->password,true);
            $criteria->compare('created',$this->created,true);

            return new CActiveDataProvider($this, array(
                'criteria'=>$criteria,
            ));
        }



        public function getNewCode(){
            $newCode = rand(0, 9).rand(0, 9).rand(0, 9).rand(0, 9);
            if($this->exists("code = '{$newCode}'")){
                return $this->getNewCode();
            }
            return $newCode;
        }

}